package com.vcms.entity.store;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//

import com.vcms.entity.store.item.StoreItem;
import com.vcms.entity.store.itemtype.StoreObject;
import com.vcms.exception.VMCSException;

public abstract class Store {

	protected int size;

	protected StoreItem items[];

	public Store() {
	}

	public Store(int itemn) {
		size = itemn;
		items = new StoreItem[size];
	}

	public void setStoreSize(int sz) {
		size = sz;
		items = new StoreItem[size];
	}

	public StoreItem[] getItems() {
		return items;
	}

	public void addItem(int idx, StoreItem object) throws VMCSException {
		if (idx >= size)
			throw new VMCSException("Store.addItem", "Index overflow");
		items[idx] = object;
	}

	public StoreItem getStoreItem(int idx) throws VMCSException {
		if (idx >= size)
			throw new VMCSException("Store.getStoreItem", "Index overflow");
		
		return items[idx];
	}

	public StoreObject findObject(String name) {
		String en;

		StoreObject so;
		int i;

		for (i = 0; i < size; i++) {
			if (items[i] == null)
				return null;
			so = items[i].getContent();
			if (so == null)
				return null;
			en = so.getName();
			if (en != null) {
				if (en.compareTo(name) == 0)
					return so;
			}
		}
		return null;
	}

	public void setQuantity(int item, int qty) throws VMCSException {
		if (item >= size)
			throw new VMCSException("Store.setQuantity", "Index overflow");
		
		items[item].setQuantity(qty);
	}

	public int getStoreSize() {
		return size;
	}

}
